package com.biz.primus.ms.payment.service;

import com.biz.primus.common.utils.DateUtil;
import com.biz.primus.common.utils.JsonUtil;
import com.biz.primus.common.utils.StringTool;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.payment.dao.po.AlipayPaymentLogPo;
import com.biz.primus.ms.payment.dao.repository.AlipayPaymentLogRepository;
import java.sql.Timestamp;
import java.util.HashMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import static com.biz.primus.ms.payment.channel.alipay.IAlipayPayment.ParamKeys.*;

@Slf4j
@Service("service.wechatPaymentLogService")
public class AlipayPaymentLogService extends AbstractBaseService {

    private final AlipayPaymentLogRepository alipayPaymentLogRepository;

    @Autowired
    public AlipayPaymentLogService(AlipayPaymentLogRepository alipayPaymentLogRepository) {
        this.alipayPaymentLogRepository = alipayPaymentLogRepository;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void logWechatPaymentNotify(HashMap<String, String> params, Long paymentOrderId) {
        try {
            String notifyId = params.get(NOTIFY_ID);
            String ourTradeNo = params.get(OUT_TRADE_NO);
            String tradeNo = params.get(TRADE_NO);
            String tradeStatus = params.get(TRADE_STATUS);
            // 该笔交易的买家付款时间。格式为 yyyy-MM-dd HH:mm:ss
            // String gmtPaymentStr = params.get(GMT_PAYMENT);
            // Long paidTime = StringUtils.isNotBlank(gmtPaymentStr) ?
            // org.codelogger.utils.DateUtils.getDateFromString(gmtPaymentStr).getTime()
            // : null;
            Integer totalFee = StringTool.formatPriceToFen(params.get(TOTAL_FEE));
            String buyerEmail = params.get(BUYER_EMAIL);
            String buyerId = params.get(BUYER_ID);
            Integer discount = StringTool.formatPriceToFen(params.get(DISCOUNT));
            Timestamp gmtCreate = params.get(GMT_CREATE) == null ? null : DateUtil.newTimestamp(params.get(GMT_CREATE));
            Timestamp gmtPayment = params.get(GMT_PAYMENT) == null ? null : DateUtil.newTimestamp(params.get(GMT_PAYMENT));
            String isTotalFeeAdjust = params.get(IS_TOTAL_FEE_ADJUST);
            Timestamp notifyTime = params.get(NOTIFY_TIME) == null ? null : DateUtil.newTimestamp(params.get(NOTIFY_TIME));
            String paymentType = params.get(PAYMENT_TYPE);
            Integer price = StringTool.formatPriceToFen(params.get(PRICE));
            Integer quantity = params.get(QUANTITY) == null ? null : Integer.parseInt(params.get(QUANTITY));
            String subject = params.get(SUBJECT);
            String useCoupon = params.get(USE_COUPON);

            log.info("global an alipay trade notify: orderPaymentId[{}], tradeno[{}], tradeStauts[{}]", ourTradeNo, tradeNo, tradeStatus);

            // 保存 alipay支付通知log
            AlipayPaymentLogPo logPo = new AlipayPaymentLogPo();
            logPo.setId(idService.getNextId());
            logPo.setOrderPaymentId(paymentOrderId);
            logPo.setTransactionId(tradeNo);
            logPo.setLog(JsonUtil.obj2Json(params));
            logPo.setBuyerEmail(buyerEmail);
            logPo.setBuyerId(buyerId);
            logPo.setDiscount(discount);
            logPo.setGmtCreate(gmtCreate);
            logPo.setGmtPayment(gmtPayment);
            logPo.setIsTotalFeeAjust(isTotalFeeAdjust);
            logPo.setNotifyId(notifyId);
            logPo.setNotifyTime(notifyTime);
            logPo.setPaymentType(paymentType);
            logPo.setPrice(price);
            logPo.setTotalFee(totalFee);
            logPo.setQuantity(quantity);
            logPo.setSubject(subject);
            logPo.setTradeStatus(tradeStatus);
            logPo.setUseCoupon(useCoupon);
            alipayPaymentLogRepository.save(logPo);
        } catch (Exception e) {
            log.info("record wechat payment log failed.", e);
        }
    }
}
